package com.zmzncs.lmtc.module.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.module.customer.dao.CardRecordDao;
import com.zmzncs.lmtc.module.customer.pojo.card_record.CardRecord;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfo;
import com.zmzncs.lmtc.module.shop.dao.GoodsFileDao;
import com.zmzncs.lmtc.module.shop.dao.ShopInfoDao;
import com.zmzncs.lmtc.module.shop.dao.ShopManagerDao;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.ShopInfo;
import com.zmzncs.lmtc.module.shop.pojo.shop_order.*;
import com.zmzncs.lmtc.module.shop.dao.ShopOrderDao;
import com.zmzncs.lmtc.module.shop.service.ShopOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.aop.aspectj.InstantiationModelAwarePointcutAdvisor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户-订单 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2021-05-20
 */
@Service
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderDao, ShopOrder> implements ShopOrderService {

    @Autowired
    ShopOrderDao shopOrderDao;
    @Autowired
    GoodsFileDao goodsFileDao;
    @Autowired
    ShopInfoDao shopInfoDao;
    @Autowired
    ParkInfoDao parkInfoDao;
    @Autowired
    CardRecordDao cardRecordDao;

    @Override
    public IPage<ShopOrderDTO> page(ShopOrderSearchForm form) {
        IPage<ShopOrderDTO> iPage = shopOrderDao.page(form.getPage(),form);
        if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0){
            for (ShopOrderDTO orderDTO : iPage.getRecords()) {

                Integer isWriteOff = orderDTO.getIsWriteOff();
                Integer payStatus = orderDTO.getPayStatus();
                if (payStatus.equals(1) && isWriteOff.equals(0)){
                    orderDTO.setPayStatus(5);
                } else if (payStatus.equals(1) && isWriteOff.equals(1)){
                    orderDTO.setPayStatus(6);
                }

                //根据商品Id查询商品图片地址
                List<String> urlList = goodsFileDao.getFileByGoodsId(orderDTO.getGoodsId());
                if (urlList != null && urlList.size() > 0){
                    orderDTO.setFileUrl(urlList.get(0));
                }
            }
        }
        return iPage;
    }

    @Override
    public ShopOrderDTO getById(Long id) {
        ShopOrderDTO shopOrderDTO = shopOrderDao.getById(id);
        if (shopOrderDTO != null){
            Integer isWriteOff = shopOrderDTO.getIsWriteOff();
            Integer payStatus = shopOrderDTO.getPayStatus();
            if (payStatus.equals(1) && isWriteOff.equals(0)){
                shopOrderDTO.setPayStatus(5);
            } else if (payStatus.equals(1) && isWriteOff.equals(1)){
                shopOrderDTO.setPayStatus(6);
            }
        }
        return shopOrderDTO;
    }

    @Override
    public IPage<ShopOrderDTO> getPayment(OrderPaymentForm form) {
        IPage<ShopOrderDTO> iPage = shopOrderDao.getPayment(form.getPage(),form);
        if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0) {
            for (ShopOrderDTO orderDTO : iPage.getRecords()) {
                if ("停车场".equals(orderDTO.getBusinessType())) {
                    //1：全天年卡，2：全天季卡，4：全天月卡，100：白天月卡，110：夜间月卡
                    if ("1".equals(orderDTO.getGoodsName())) {
                        orderDTO.setGoodsName("全天年卡");
                    } else if ("2".equals(orderDTO.getGoodsName())) {
                        orderDTO.setGoodsName("全天季卡");
                    } else if ("4".equals(orderDTO.getGoodsName())) {
                        orderDTO.setGoodsName("全天月卡");
                    } else if ("100".equals(orderDTO.getGoodsName())) {
                        orderDTO.setGoodsName("白天月卡");
                    } else if ("110".equals(orderDTO.getGoodsName())) {
                        orderDTO.setGoodsName("夜间月卡");
                    }
                }
            }
        }
        return iPage;
    }

    @Override
    public Map<String,BankInfoVo> getBankInfo(OrderPaymentSelectForm form) {
        Map<String,BankInfoVo> map = new HashMap<>();

        if ("商户".equals(form.getBusinessType())) {
            BankInfoVo shop = new BankInfoVo();
            ShopInfo shopInfo = shopInfoDao.selectById(form.getShopId());
            shop.setAccountName(shopInfo.getAccountName());
            shop.setBankAccount(shopInfo.getBankAccount());
            shop.setBankName(shopInfo.getBankName());
            map.put("shop",shop);

            if (form.getParkId() != null) {
                BankInfoVo park = new BankInfoVo();
                ParkInfo parkInfo = parkInfoDao.selectById(form.getParkId());
                park.setBankName(parkInfo.getBankName());
                park.setBankAccount(parkInfo.getBankAccount());
                park.setAccountName(parkInfo.getAccountName());
                map.put("park",park);
            } else {
                map.put("park",null);
            }

        } else if ("停车场".equals(form.getBusinessType())) {
            BankInfoVo park = new BankInfoVo();
            ParkInfo parkInfo = parkInfoDao.selectById(form.getParkId());
            park.setBankName(parkInfo.getBankName());
            park.setBankAccount(parkInfo.getBankAccount());
            park.setAccountName(parkInfo.getAccountName());
            map.put("park",park);
            map.put("shop",null);
        }
        return map;
    }

    @Override
    public void updatePayment(OrderPaymentUpdateForm form) {
        if ("商户".equals(form.getBusinessType())) {
            ShopOrder shopOrder = new ShopOrder();
            shopOrder.setId(form.getId());
            shopOrder.setPaidToShopTradeNo(form.getPaidToShopTradeNo());
            shopOrder.setPaidToParkTradeNo(form.getPaidToParkTradeNo());
            shopOrder.setIsPaidToShop(1);
            shopOrder.setIsPaidToPark(1);
            shopOrderDao.updateById(shopOrder);
        } else if ("停车场".equals(form.getBusinessType())) {
            CardRecord cardRecord = new CardRecord();
            cardRecord.setId(form.getId());
            cardRecord.setPaidToParkTradeNo(form.getPaidToParkTradeNo());
            cardRecord.setIsPaidToPark(1);
            cardRecordDao.updateById(cardRecord);
        }
    }
}
